﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessModel.EntityModel;
using MemCached;

namespace BusinessModel.StudyManager
{
    public class CourseCateHelper
    {
        private string mcName = "el_coursecategory";
        private CMemCached mc = new CMemCached();

        public List<CourseCateEn> GetAllCourseEn()
        {
            List<CourseCateEn> resCourseCateEn;
            //缓存判断
            if (!mc.KeyExist(mcName))
            {
                //从数据库获取数据
                EntityOprHelper enOp = new EntityOprHelper();

                resCourseCateEn = enOp.GetEntityList<CourseCateEn>(string.Empty);
                mc.SetObject(mcName, resCourseCateEn);
            }
            else
            {
                resCourseCateEn = mc.GetObject(mcName) as List<CourseCateEn>;
            }

            return resCourseCateEn;
        }

        public string AddCourseCate(CourseCateEn ccEn)
        {
            string message = "添加成功！";
            EntityOprHelper eoHelper = new EntityOprHelper();
            CourseCateEn en = eoHelper.GetEntity<CourseCateEn>(ccEn.category_id);
            if (en != null)
                message = "课程分类编码已存在！";
            bool flag = eoHelper.AddEntity<CourseCateEn>(ccEn);
            if (!flag)
                message = "添加失败！";
            return message;
        }

        public string EditCourseCate(CourseCateEn ccEn)
        {
            string message = "修改成功！";
            EntityOprHelper eoHelper = new EntityOprHelper();
            CourseCateEn en = eoHelper.GetEntity<CourseCateEn>(ccEn.category_id);
            if (en == null)
                message = "该记录已不存在！";
            bool flag = eoHelper.EditEntity<CourseCateEn>(ccEn);
            if (!flag)
                message = "修改失败！";
            return message;
        }

        public string DeleteCourseCate(string cateID)
        {
            string message = "删除成功！"; 
            EntityOprHelper eoHelper = new EntityOprHelper();
            string sql = string.Format("Delete from el_coursecategory Where category_id = '{0}' or parent_id='{0}'",
                cateID);
            if (!eoHelper.ExecuteNonQuery(sql))
            {
                message = "删除失败！";
            }
            return message;
        }
    }
}
